package com.aaa.system.controller.api;

import com.aaa.entity.Admin;
import com.aaa.security.annotation.HasPermission;
import com.aaa.security.util.WebUtils;
import com.aaa.system.service.AdminService;
import com.aaa.util.JwtUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @program: qy158-cai-parent
 * @description: 提供给前端的接口
 * @author: 闫克起2
 * @create: 2023-02-22 17:14
 **/
@RestController
@RequestMapping("system/api/admin")
@Slf4j
public class ApiAdminController {
    @Autowired
    private AdminService adminService;

    @GetMapping("query")
    @HasPermission(value = "admin:query")
    public String query(){
        return "admin:query";
    }

    @GetMapping("insert")
    @HasPermission(value = "admin:insert")
    public String insert(){
        return "admin:insert";
    }

    @GetMapping("update")
    @HasPermission(value = "admin:update")
    public String update(){
        return "admin:update";
    }

    @GetMapping("/info")
    public Result<Admin> info(){
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        String username = (String) tokenChaim.get("username");
        QueryWrapper<Admin> wrapper=new QueryWrapper<>();
        wrapper.eq("admin_username",username);
        Admin admin = adminService.getOne(wrapper);
        return new Result<>(2000,"查询用户信息",admin);
    }

    @Autowired
    private StringRedisTemplate redisTemplate;
    @GetMapping("logout")
    public Result<String> logout(){
        String token = WebUtils.getRequest().getHeader("token");
        redisTemplate.delete(token);
        return new Result<>(2000,"退出成功");
    }
}





















